package com.wanmait.shoppingsys.controller.web;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wanmait.shoppingsys.dao.AddressDAO;
import com.wanmait.shoppingsys.dao.UserinfoDAO;
import com.wanmait.shoppingsys.vo.Address;
import com.wanmait.shoppingsys.vo.UserInfo;


@WebServlet("/AddressController")
public class AddressController extends HttpServlet{
	private AddressDAO addressDAO =AddressDAO.getDAO();
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");
		
		if(action==null) return;
		
		switch(action) 
		{
		case "add":                     //添加地址
			this.add(request, response);
			break;
		case "findAll":					//查询全部
			this.findAll(request, response);
			break;
		case"addA":
			this.addA(request, response);
			break;
		case "delet":
			this.delet(request, response);
			break;
		case "delett":
			this.delett(request, response);
			break;	
		}
	}
	
protected void delett(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String path = request.getServletContext().getContextPath();
		  String idd = request.getParameter("id");
		  Integer id = Integer.parseInt(idd);
		  addressDAO.delete(id);
		 response.sendRedirect(path+"/order/add.jsp");
	}

protected void delet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String path = request.getServletContext().getContextPath();
		  String idd = request.getParameter("id");
		  Integer id = Integer.parseInt(idd);
		  addressDAO.delete(id); 
		response.sendRedirect(path+"/userinfo/address.jsp");
	}
protected void addA(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String path = request.getServletContext().getContextPath();
		
		  String province   = request.getParameter("province");
		  String city	 = request.getParameter("city");
		  String district = request.getParameter("district");
		  					
		   
		 String namea  = request.getParameter("namea");
		 String site = request.getParameter("site");
		 String cpn = request.getParameter("cpn");
		 String idd = request.getParameter("id");
		 Integer id = Integer.parseInt(idd);
		 Address address = new Address();
		 address.setNamea(namea);
		 address.setSite(site);
		 address.setCpn(cpn);
		 address.setId(id);
		 address.setProvince(province);
		 address.setCity(city);
		 address.setDistrict(district);
		 addressDAO.insert(address);
		 response.sendRedirect(path+"/order/order.jsp"); 
	}
	protected void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			  UserInfo userinfo   = (UserInfo)	request.getSession().getAttribute("userinfo");
			  Integer id = userinfo.getId();
			  ArrayList<Address> address = new ArrayList<Address>();
			  address  = addressDAO.findAll(id);
			  request.setAttribute("address", address);
			
	}
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String path = request.getServletContext().getContextPath();
		
		
		

		  String province   = request.getParameter("province");
		  String city	 = request.getParameter("city");
		  String district = request.getParameter("district");    
		  
		  String namea  = request.getParameter("namea");
		  String site = request.getParameter("site");
		  String cpn = request.getParameter("cpn");
		  String idd   = request.getParameter("id");
		  Integer id    = Integer.parseInt(idd);
		  Address address= new Address();
		  address.setNamea(namea);
		  address.setSite(site);
		  address.setCpn(cpn);
		  address.setId(id);
		  address.setProvince(province);
		  address.setCity(city);
		  address.setDistrict(district);
	
		 addressDAO.insert(address);
		 response.sendRedirect(path+"/userinfo/address.jsp?err=1");	 
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}
	
}
